/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.util.*;

public class BindableProperties implements Bindable {
    protected Properties properties = null;
    private BindFieldInterface [] bindfields = new  BindFieldInterface[]{};
    
    public BindableProperties() {
        properties = new Properties();
    }
    
    public String getSourceName() {return "";}
    
    public Properties getProperties() {return properties;}
    
    public void setProperties(Properties p_properties)
    throws BindException {
        properties=p_properties;
        reload();
    }
    
    public void removeAllFields() {
        bindfields = new  BindFieldInterface[]{};
    }
    
    public BindFieldInterface [] getBindFields()
    throws BindException {
        return (BindFieldInterface[]) bindfields;
    }
    
    public boolean find(BindFieldInterface bind_fields[],boolean allow_multiple_rows,boolean match_on_any)
    throws BindException {
        throw new BindException("Not implemented yet");
    }
    
    public VectorBindable getListOfAllBindables()
    throws BindException {
        throw new BindException("Not implemented yet");
    }
    
    public void addField(BindFieldInterface p_field) {
        // grow array
        BindFieldInterface temp_field[] = new BindFieldInterface[bindfields.length + 1];
        for (int i = 0;i < bindfields.length;i++) {
            temp_field[i] = bindfields[i];
        }
        temp_field[temp_field.length - 1] = (BindFieldInterface) p_field;
        bindfields = temp_field;
    }
    
    public void setBindFields(BindFieldInterface [] p_bindfields)
    throws BindException {
        bindfields = p_bindfields;
        //reload();
    }
    
    public boolean shouldSave() {return true;}
    
    public void save()
    throws BindException {
        saveBindable();
    }
    
    public void commit() throws BindException {}
    
    public boolean saveBindable()
    throws BindException {
        for (int i = 0;i < bindfields.length;i++) {
            if (bindfields[i].getSerializable() != null) {
                properties.setProperty(bindfields[i].getName(),bindfields[i].getSerializable());
            }
        }
        return true;
    }
    
    public void delete()
    throws BindException {
        properties = new Properties();
    }
    
    public void reload()
    throws BindException {
        for (int i = 0;i < bindfields.length;i++) {
            bindfields[i].setSerializable(properties.getProperty(bindfields[i].getName()));
        }
    }
    
    public void loadFrom(Properties p_store)
    throws BindException {
        Properties store = new Properties();
        for (int i = 0;i < bindfields.length;i++) {
            if (p_store.getProperty(bindfields[i].getName()) != null) {
                store.put(bindfields[i].getName(),p_store.getProperty(bindfields[i].getName()));
                bindfields[i].setSerializable(p_store.getProperty(bindfields[i].getName()));
            }
        }
        setProperties(store);
    }
    
    public boolean isLoaded()
    throws BindException {
        return true;
    }
    
    public Bindable getNextBindable()
    throws BindException {
        return null;
    }
    
    public Bindable getNewBindable()
    throws BindException {
        return new BindableProperties();
    }
    
}

